package com.android.pI;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class LogEditActivity extends Activity {
	private Bundle bundle;
	private EditText mDate;
	TextView title;
	private EditText mRep;
	private EditText mBands;
	private EditText mWeight;
	private EditText mNotes;
	private Cursor cursor;
	private String selectedItem;
	private DataBaseHelper mDbHelper;
	protected ArrayAdapter<CharSequence> mAdapter;
	SimpleCursorAdapter adapter;
	private Spinner spinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDbHelper = new DataBaseHelper(this);
		setContentView(R.layout.note_edit);
		mDate = (EditText) findViewById(R.id.date_notes);
		mRep = (EditText) findViewById(R.id.reps_notes);
		mBands = (EditText) findViewById(R.id.bands_notes);
		mWeight = (EditText) findViewById(R.id.weight_notes);
		mNotes = (EditText) findViewById(R.id.notes_notes);
		mDate.setText((String) android.text.format.DateFormat.format(
				"MM/dd/yyyy", new java.util.Date()));

		checkCase();

		spinner = (Spinner) findViewById(R.id.Spinner01);
		cursor = mDbHelper.fetchAllExs();
		startManagingCursor(cursor);
		// Setup Adapter
		String[] from = { "ex_name" };
		int[] to = { R.id.spin1 };
		adapter = new SimpleCursorAdapter(this, R.layout.spinner_row, cursor, from, to);

		spinner.setAdapter(adapter);

		Button confirmButton = (Button) findViewById(R.id.confirm);
		confirmButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				setResult(RESULT_OK);
				saveState();
				finish();
			}

		});
		
		//set spinner on select listener
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				cursor.moveToPosition(position);
				setI(cursor.getString(cursor.getColumnIndex("_id")));

			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		// set spinner to default
	    List<String> listItems = new ArrayList<String>();
	    cursor.moveToFirst();
	    do {
	        String name = cursor.getString(cursor.getColumnIndex("ex_name"));
	        listItems.add(name);
	        } while(cursor.moveToNext());
		
	    ArrayAdapter<String> notes = new ArrayAdapter<String>(this, R.layout.notes_row, listItems);
		String myString = bundle.getString("ex_name");
		int spinnerPosition = notes.getPosition(myString);
		spinner.setSelection(spinnerPosition, true);
		cursor.moveToPosition(spinnerPosition);
		setI(cursor.getString(cursor.getColumnIndex("_id")));
	}



	private void populate(String lId) {

		Cursor log = mDbHelper.fetchLogId(lId);
		startManagingCursor(log);
		setI(bundle.getString("e_id"));
		mDate.setText(log.getString(log.getColumnIndex("date")));
		mRep.setText(log.getString(log.getColumnIndex("reps")));
		mBands.setText(log.getString(log.getColumnIndex("bands")));
		mWeight.setText(log.getString(log.getColumnIndex("weight")));
		mNotes.setText(log.getString(log.getColumnIndex("notes")));
		mDbHelper.close();
	}

	private void checkCase() {
		if (LogEditActivity.this.getIntent().getExtras() != null) {
			bundle = this.getIntent().getExtras();// get the intent & bundle
													// passed by X
			/*
			 * show the txt in a Toast, and of course u can show it anywhere
			 * else u want by calling Bundle.getString (String key)
			 */
			int cCase = bundle.getInt("case");
			if (cCase == 1) {
				String logId = bundle.getString("log_id");
				populate(logId);
			} else
				getTxt();
		}
	}

	private void getTxt() {
		if (LogEditActivity.this.getIntent().getExtras() != null) {
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveState();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	public void setI(String i) {
		selectedItem = i;
	}

	public String getI() {
		return selectedItem;
	}

	private void saveState() {
		String ld = bundle.getString("log_id");
		String date = mDate.getText().toString();
		String rep = mRep.getText().toString();
		String bands = mBands.getText().toString();
		String w = mWeight.getText().toString();
		String notes = mNotes.getText().toString();
		String eid = getI();

		int cCase = bundle.getInt("case");
		if (cCase == 1) {
			mDbHelper.UpdateLog(ld, eid, w, bands, rep, notes, date);
		} else {
			mDbHelper.createLog(eid, w, bands, rep, notes, date);
		}

		mDbHelper.close();
	}

}
